<?php

namespace app\api\controller\wanlshop;

use app\admin\model\wanlshop\AddressSpot;
use app\admin\model\wanlshop\Auth;
use app\common\controller\Api;
use fast\Tree;
/**
 * WanlShop店铺接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['getShopInfo','user_status','endpointShop'];
    protected $noNeedRight = ['*'];
	
	public function _initialize()
	{
	    parent::_initialize();
		$this->model = model('app\api\model\wanlshop\Shop');
	}
	
	/**
	 * 一次性获取店铺相关数据 1.0.8升级
	 *
	 * @ApiSummary  (WanlShop 一次性获取店铺相关数据)
	 * @ApiMethod   (GET)
	 *
	 * @param string $id 页面ID
	 */
	public function getShopInfo($id = null)
	{
		//设置过滤方法
		$this->request->filter(['strip_tags']);
		// 获取店铺信息
		$row = $this->model->get($id);
		if (!$row) {
		    $this->error(__('未找到此商家'));
		}
		// 获取商家类目
		$tree = Tree::instance();
		$tree->init(
			model('app\api\model\wanlshop\ShopSort')
				->where(['shop_id' => $row['id']])
				->field('id, pid, name, image')
				->order('weigh asc')
				->select()
		);
		$row['category'] = $tree->getTreeArray(0);
		// 查看是否被关注
		$row['isFollow'] = model('app\api\model\wanlshop\find\Follow')
			->where([
				'user_no' => $row['find_user']['user_no'], 
				'user_id' => $this->auth->id
			])
			->count();
		$row['isLive'] = model('app\api\model\wanlshop\Live')
			->where(['shop_id' => $row['id'], 'state' => 1])
			->field('id')
			->find();
		// 获取类目样式配置
		$shopConfig = model('app\api\model\wanlshop\ShopConfig')
			->where(['shop_id' => $row['id']])
			->find();
		$row['categoryStyle'] = (int)$shopConfig['category_style'];
		// 获取商家自定义页面
		$row['page'] = model('app\api\model\wanlshop\Page')
			->where([
				'shop_id' => $row['id'], 
				'type' => 'shop'
			])
			->field('id, name, page, item')
			->find();
		$this->success('返回成功', $row);
	}


    public function user_status()
    {
        $user_id = $this->auth->id;
        $phone = $this->auth->mobile;
//        halt($user_id);
        $data = [];
        $type = $this->request->post('type',1);
        if($type == 1){
            $auth = Auth::where(['user_id'=>$user_id])->whereNull('deletetime')->find();
            if(!empty($auth)){
                if($auth['verify'] == 3){
                    $shop = \app\admin\model\wanlshop\Shop::where(['user_id'=>$user_id])->whereNull('deletetime')->find();
                    if(empty($shop)){
                        $data['shop_status'] = -1;
                        $data['refuse'] = '';
                        $data['spot_status'] =  -1;
                        $data['fail_reason'] = '';
                        $data['wx_status'] = -1;
                    }else{
                        $data['wx_status'] = $this->endpoint($shop['id']);
                        if(   $data['wx_status'] == '审核中'
                            ||$data['wx_status'] == '待账户验证'
                            ||$data['wx_status'] == '待签约'
                            ||$data['wx_status'] == '开通权限中'
                        ){
                            $data['shop_status'] =  2;
                        }elseif($data['wx_status'] == '已完成' && $shop['verify'] == 3){
                            $data['shop_status'] =  3;
                        }else{
                            $data['shop_status'] = 4;
                        }
                        $data['refuse'] = $data['wx_status'];
                    }
                }else if($auth["verify"] == 4){
                    $data['shop_status'] = $auth["verify"];
                    $data['refuse'] = $auth["refuse"];
                    $data['spot_status'] =  -1;
                    $data['fail_reason'] = '';
                }else if($auth["verify"] == 2){
                    $data['shop_status'] = $auth["verify"];
                    $data['refuse'] = $auth["refuse"];
                    $data['spot_status'] =  -1;
                    $data['fail_reason'] = '';
                }



            }else{
                $data['shop_status'] =  -1;
            }
        }else{
            $spot = AddressSpot::where(['phone'=>$phone,'type'=>1,'shop_id'=>0])->whereNull('deletetime')->find();
            if(empty($spot)){
                $data['spot_status'] = -1;
                $data['fail_reason'] = '';
            }else{
                $data['wx_status'] = $this->endpoint($spot['id'],2);
                if(   $data['wx_status'] == '审核中'
                    ||$data['wx_status'] == '待账户验证'
                    ||$data['wx_status'] == '待签约'
                    ||$data['wx_status'] == '开通权限中'
                ){
                }elseif($data['wx_status'] == '已完成'){
                }else{
                    $data['fail_reason'] = $data['wx_status'];

                }
                $data['spot_status'] =  $spot['status'];

            }
        }




        $this->success('状态',$data);


    }

    public function endpoint($shop_id,$type=1)
    {
        if($type = 2){
            $row =  AddressSpot::where('id',$shop_id)->find();
        }else{
            $row = $this->model->get($shop_id);
        }

        $separte = new Separate();
        $res = $separte->QueryApply($row['applyment_id']);

        switch ($res['applyment_state']){
            case 'APPLYMENT_STATE_EDITTING':
                $text = '编辑中,提交申请发生错误导致，请尝试重新提交';
                break;
            case 'APPLYMENT_STATE_AUDITING':
                $text = '审核中';
                break;
            case 'APPLYMENT_STATE_REJECTED':
                if($res['applyment_state_msg']){
                    $msg = $res['applyment_state_msg'];
                }else{
                    $msg = $res['audit_detail'][0]['reject_reason'];
                }
                $text = $msg;
                break;
            case 'APPLYMENT_STATE_TO_BE_CONFIRMED':
                $text = '待账户验证';
                break;
            case 'APPLYMENT_STATE_TO_BE_SIGNED':
                $text = '待签约';
                break;
            case 'APPLYMENT_STATE_SIGNING':
                $text = '开通权限中';
                break;
            case 'APPLYMENT_STATE_FINISHED':
                $text = '已完成';
                break;
        }
        return $text;
    }


    public function endpointShop()
    {
        $shops = \app\api\model\wanlshop\Shop::whereNull('sub_mchid')->select();
        $separte = new Separate();
        foreach ($shops as $value){
            $res = $separte->QueryApply($value['applyment_id']);
            if(isset($res['sub_mchid'])){
                \app\api\model\wanlshop\Shop::where('id',$value['id'])->update([
                    'sub_mchid'=>$res['sub_mchid']
                ]);
            }
            switch ($res['applyment_state']){
                case 'APPLYMENT_STATE_EDITTING':
                    $text = '编辑中,提交申请发生错误导致，请尝试重新提交';
                    \app\api\model\wanlshop\Shop::where('id',$value['id'])->update([
                        'status'=>'hidden'
                    ]);
                    break;
                case 'APPLYMENT_STATE_AUDITING':
                    $text = '审核中';
                    break;
                case 'APPLYMENT_STATE_REJECTED':
                    if($res['applyment_state_msg']){
                        $msg = $res['applyment_state_msg'];
                    }else{
                        $msg = $res['audit_detail'][0]['reject_reason'];
                    }
                    \app\api\model\wanlshop\Shop::where('id',$value['id'])->update([
                        'status'=>'hidden'
                    ]);
                    $text = '已驳回,'.$msg;
                    break;
                case 'APPLYMENT_STATE_TO_BE_CONFIRMED':
                    $text = '待账户验证';
                    break;
                case 'APPLYMENT_STATE_TO_BE_SIGNED':
                    $text = '待签约';
                    break;
                case 'APPLYMENT_STATE_SIGNING':
                    $text = '开通权限中';
                    break;
                case 'APPLYMENT_STATE_FINISHED':
                    $text = '已完成';
                    break;
            }
        }


    }
}